﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.DataService
{
    public class OfficeService : IOfficeRepository
    {

        private DataContext context = new DataContext();

        #region IOfficeRepository Members

        public IList<int> GetUnitList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from office in context.tb_Office
                            group office by office.tb_Unit.Id into unit
                            select unit.Key).ToList();

                return list;
            }
        }
        #endregion

        #region IRecordOperation<tb_Office,OfficeVm> Members

        public IList<OfficeVm> GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from office in context.tb_Office
                            select new OfficeVm()
                            {
                                Id = office.Id,
                                Name = office.Name,
                                Unit = new UnitVm() { Id = office.tb_Unit.Id, Name = office.tb_Unit.Name },
                                State = new StateVm() { Id = office.tb_State.Id, Name = office.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        public void New(tb_Office entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Office(entity);
                context.SaveChanges();
            }
        }
        public OfficeVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from office in context.tb_Office
                            where office.Id == id
                            select new OfficeVm()
                            {
                                Id = office.Id,
                                Name = office.Name,
                                Phone = office.Phone,
                                Fax = office.Fax,
                                Mail = office.Mail,
                                Address = office.Address,
                                Unit = new UnitVm() { Id = office.tb_Unit.Id, Name = office.tb_Unit.Name },
                                State = new StateVm() { Id = office.tb_State.Id }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Office entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from office in context.tb_Office
                            where office.Id == id
                            select office).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Phone = entity.Phone;
                edit.Fax = entity.Fax;
                edit.Mail = entity.Mail;
                edit.Address = entity.Address;
                edit.tb_UnitReference.EntityKey = entity.tb_UnitReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from office in context.tb_Office
                              where office.Id == id
                              select office).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IFindOperation<OfficeVm> Members

        public IList<OfficeVm> GetList(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from office in context.tb_Office
                            where office.Name.StartsWith(findName)
                            select new OfficeVm()
                            {
                                Id = office.Id,
                                Name = office.Name,
                                Unit = new UnitVm() { Id = office.tb_Unit.Id, Name = office.tb_Unit.Name },
                                State = new StateVm() { Id = office.tb_State.Id, Name = office.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        #endregion

        #region IRowCountOperation Members

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from office in context.tb_Office
                                select office).Count();

                return rowCount;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from office in context.tb_Office
                                where office.Name.StartsWith(findName)
                                select office).Count();

                return rowCount;
            }
        }
        #endregion

        #region IParentRelationshipOperation Members

        public IList<int> GetCounts()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from office in context.tb_Office
                                 group office by office.tb_Unit.Id into unit
                                 select unit.Count()).ToList();

                return countList;
            }
        }
        public IList<int> GetCounts(string find)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from office in context.tb_Office
                                 where office.Name.StartsWith(find)
                                 group office by office.tb_Unit.Id into unit
                                 select unit.Count()).ToList();

                return countList;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from office in context.tb_Office
                             where office.Id == id
                             select office).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from office in context.tb_Office
                             where office.Id == id
                             select office).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

    }
}
